package com.nit.lab.vote.utils;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * @author F
 * @project export
 * @className ExportUtil
 * @description Export工具类
 * @date 2020/12/16
 **/
public class ExportUtil {

    /**
     * 数据导出成 Excel 数据格式
     *
     * @param headList 表头信息 （为null 或者 size() == 0 证明不存在）
     * @param dataList 数据信息
     * @param xlsType  excel文件类型 03版为 xls 07版为 xlsx 默认 xlsx
     * @return 结果数组
     */
    public static byte[] exportExcel(List<String> headList, List<List<String>> dataList, String xlsType) {
        byte[] bytes = null;
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            Workbook workbook;
            if (StringUtils.endsWithIgnoreCase(xlsType, "xls")) {
                workbook = new XSSFWorkbook();
            } else {
                workbook = new HSSFWorkbook();
            }
            int size = 65530;
            int sheetCount = dataList == null ? 0 : dataList.size() % size;

            // 生成一个表格
            Sheet sheet = workbook.createSheet("132");
            // 调整列宽度
            sheet.setDefaultColumnWidth(13);
//            for (int i = 0; i < sheetCount; i++) {

                int rowIndex = 0;
                if (headList != null && headList.size() > 0) {
                    writerHeader(headList, sheet, xlsType);
                }
                rowIndex++;
                writerData(dataList, sheet, rowIndex, xlsType);
//            }

            workbook.write(os);
            bytes = os.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bytes;
    }


    /**
     * 写表头
     *
     * @param headList 表头List
     * @param sheet    sheet
     * @param xlsType  类型
     */
    private static void writerHeader(List<String> headList, Sheet sheet, String xlsType) {

        Row row = sheet.createRow(0);
        row.setHeightInPoints(21);
        RichTextString text;
        for (int i = 0; i < headList.size(); i++) {
            Cell cell = row.createCell((short) i);
            if (StringUtils.endsWithIgnoreCase(xlsType, "xls")) {
                text = new XSSFRichTextString(headList.get(i));
                cell.setCellValue(text.toString());
            } else {
                text = new HSSFRichTextString(headList.get(i));
                cell.setCellValue(text.toString());
            }
        }
    }

    /**
     * 写数据
     *
     * @param dataList 数据集
     * @param sheet    sheet
     * @param rowIndex 行号
     * @param xlsType  类型
     */
    private static void writerData(List<List<String>> dataList, Sheet sheet, int rowIndex, String xlsType) {
        Row row;
        Cell cell;
        RichTextString text;
        for (int i = 0; i < dataList.size(); i++) {
            row = sheet.createRow(rowIndex + i);
            List<String> datas = dataList.get(i);
            for (int j = 0; j < datas.size(); j++) {
                cell = row.createCell(j);
                String data = datas.get(j);
                if (StringUtils.endsWithIgnoreCase(xlsType, "xls")) {
                    text = new XSSFRichTextString(data != null ? data : "");
                    cell.setCellValue(text);
                } else {
                    text = new HSSFRichTextString(data != null ? data : "");
                    cell.setCellValue(text.toString());
                }
            }
        }
    }

}
